package com.banzhi.shop.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.data.GroupsBean;
import com.banzhi.shop.data.MyGroupingBean;
import com.banzhi.shop.data.MyGroupingData;
import com.banzhi.shop.data.PinTuanBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.banzhi.shop.MyApp.context;

/**
 * Created by Administrator on 2018/7/23.
 */
//我的拼团适配器
public class MyGroupingAdapter extends RecyclerView.Adapter<MyGroupingAdapter.ViewHolder> {
    private List<PinTuanBean> mList;
    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        View myView;
        ImageView imageView;
        TextView title;
        TextView groupsPrice;
        TextView mTvTime;
        TextView mTvStatus1;
        TextView mTvStatus2;
        TextView mTvStatus3;
        Button mBt1;
        Button mBt2;
        Button mButton11;
        LinearLayout mLlItem;
        Button mBt4;
        Button mBt5;
        Button mBt6;
        Button mBt7;
        Button mBt8;
        public ViewHolder(View itemView) {
            super(itemView);
            myView = itemView;
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time);
            mTvStatus1 = (TextView) itemView.findViewById(R.id.tv_status1);
            mTvStatus2 = (TextView) itemView.findViewById(R.id.tv_statu2);
            mTvStatus3 = (TextView) itemView.findViewById(R.id.tv_statu3);
            imageView = (ImageView) itemView.findViewById(R.id.iv_thumb);
            title = (TextView) itemView.findViewById(R.id.tv_title_grouping);
            groupsPrice = (TextView) itemView.findViewById(R.id.tv_groupsprice);

            mBt1 = (Button) itemView.findViewById(R.id.bt1);
            mBt2 = (Button) itemView.findViewById(R.id.bt2);
            mBt4 = (Button) itemView.findViewById(R.id.bt4);
            mBt5 = (Button) itemView.findViewById(R.id.bt5);
            mBt6 = (Button) itemView.findViewById(R.id.bt6);
            mBt7 = (Button) itemView.findViewById(R.id.bt7);
            mBt8 = (Button) itemView.findViewById(R.id.bt8);

            mButton11 = (Button) itemView.findViewById(R.id.bt11);
            mLlItem = (LinearLayout) itemView.findViewById(R.id.ll_item);

        }
    }

    public MyGroupingAdapter(List<PinTuanBean> list) {
        this.mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_grouping, null);
        final ViewHolder holder = new ViewHolder(view);
        //条目点击控件
        holder. mLlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }
            }
        });
        holder.mBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // int position = holder.getAdapterPosition();
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });
        holder.mBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });
        holder.mBt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }
            }
        });
        holder.mBt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });
        holder.mBt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });
        holder.mBt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PinTuanBean bean = mList.get(position);
        holder.title.setText(bean.getTitle());
        holder.groupsPrice.setText(bean.getGroupsprice());
        holder.mTvTime.setText(bean.getCreatetime());
        Picasso.with(context).load(bean.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
       // bt4申请售后 bt5待发货 bt6确认收货
        if ("0".equals(bean.getDispart())) {
            holder.mTvStatus1.setText("拼团成功");
            holder.mTvStatus2.setVisibility(View.GONE);
            holder.mTvStatus3.setVisibility(View.GONE);
            holder.mBt2.setVisibility(View.GONE);
            holder.mButton11.setVisibility(View.INVISIBLE);
            //type==2
            if ("2".equals(bean.getType())) {
                if ("1".equals(bean.getStatus())) {             //待发货
                    holder.mBt5.setVisibility(View.VISIBLE);
                    holder.mBt4.setVisibility(View.GONE);
                    holder.mBt6.setVisibility(View.GONE);
                    holder.mTvStatus3.setVisibility(View.GONE);
                } else if ("2".equals(bean.getStatus())) {      //确认收货
                    holder.mBt5.setVisibility(View.GONE);
                    holder.mBt4.setVisibility(View.GONE);
                    holder.mBt6.setVisibility(View.VISIBLE);
                }else if ("3".equals(bean.getStatus())) {//0申请中  1已完成 -1已拒绝 -2已取消 345售后处理中
                    if (bean.getRefundid()>0) {
                        if ("1".equals(bean.getRefundstatus())) {
                            holder.mTvStatus1.setText("售后完成");
                        }else if ("-1".equals(bean.getRefundstatus())){
                            holder.mTvStatus1.setText("售后拒绝");
                            holder.mBt4.setVisibility(View.GONE);
                            holder.mBt7.setVisibility(View.VISIBLE);
                            holder.mBt6.setVisibility(View.GONE);
                            holder.mBt5.setVisibility(View.GONE);
                        } else  {
                            holder.mBt7.setVisibility(View.VISIBLE);//申请维权中
                            holder.mBt6.setVisibility(View.GONE);
                            holder.mBt5.setVisibility(View.GONE);
                            holder.mBt4.setVisibility(View.GONE);
                            holder.mTvStatus1.setText("售后维权中");
                            holder.mTvStatus3.setVisibility(View.GONE);
                        }


                    } /*else  {
                        holder.mBt5.setVisibility(View.GONE);
                        holder.mBt4.setVisibility(View.GONE);
                        holder.mBt6.setVisibility(View.GONE);
                        holder.mBt7.setText("已完成");
                    }*/
                } else if ("-1".equals(bean.getStatus())) {      //已取消
                    //当存在refundid并且大于0
                    if (bean.getRefundid()>0 ) {
                        holder.mBt8.setText("售后完成");
                        holder.mBt8.setVisibility(View.VISIBLE);
                        holder.mBt6.setVisibility(View.GONE);
                        holder.mBt5.setVisibility(View.GONE);
                        holder.mTvStatus3.setVisibility(View.GONE);
                        holder.mBt4.setVisibility(View.GONE);
                    }else {
                        holder.mBt8.setVisibility(View.VISIBLE);
                        holder.mBt6.setVisibility(View.GONE);
                        holder.mBt5.setVisibility(View.GONE);
                        holder.mTvStatus3.setVisibility(View.GONE);
                        holder.mBt4.setVisibility(View.GONE);
                    }

                }
            }
        } else if ("-1".equals(bean.getDispart())) {
            holder.mTvStatus1.setText("拼团失败");
            holder.mTvStatus2.setVisibility(View.GONE);
            holder.mTvStatus3.setVisibility(View.GONE);
            holder.mBt2.setVisibility(View.GONE);
            holder.mButton11.setVisibility(View.INVISIBLE);
            holder.mBt4.setVisibility(View.GONE);
            holder.mTvStatus3.setVisibility(View.GONE);
        } else {
            holder.mTvStatus1.setVisibility(View.VISIBLE);
            holder.mTvStatus2.setText(bean.getDispart() + "");
            holder.mTvStatus3.setVisibility(View.VISIBLE);
            holder.mBt4.setVisibility(View.GONE);

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
        holder.mBt1.setTag(position);
        holder.mBt2.setTag(position);
        holder.mBt4.setTag(position);
        holder.mBt6.setTag(position);
        holder.mBt7.setTag(position);
        holder.mBt8.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    //下面两个方法提供给页面刷新和加载时调用
    public void add(List<PinTuanBean> addMessageList) {
        //增加数据
        int position = mList.size();
        mList.addAll(position, addMessageList);
        notifyItemInserted(position);
    }

    public void refresh(List<PinTuanBean> newList) {
        //刷新数据
        mList.removeAll(mList);
        mList.addAll(newList);
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(MyGroupingAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


}
